/**
 * Created by liujie on 2017/12/3.
 */
var express = require('express');
var router = express.Router();
var auth = require('../auth/auth');

var url = require('url');
var qstr = require("querystring");
var pro = require('../property/cuspro');
var util = require('../util/commonutil');
var request = require('request');
var resultJSON = require('../routes/common/resultJSON');
/* GET home page. */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Expr问问ess22-444' });
// });

//测试菜单
var items = [{title: '文章管理'}, {title: '人员管理'}];
// 测试用
router.get('/menus', function (req, res, next) {
    //ajax请求返回user对象
    // var user=null;
    // auth.dologin(req, res, next,user);
    var menus=[{"name":"监控管理","icon":"","url":"#"}];
    var menuItems=[
        {"name":"实时监控","icon":"","url":"/map"},
        {"name":"轨迹回放","icon":"","url":"/map/his"},
    ];
    res.render('main/index', {title: '菜单', items: items});
});
router.post('/index', function (req, res, next) {
    //ajax请求返回user对象
    // var user=null;
    // auth.dologin(req, res, next,user);
    var sid=auth.getSessionID(req, res, next);
    var qs=req.body;
    qs.sid=sid;
    //res.redirect('/index');
    // {serverurl:pro.serveraddr,menus:data.parentMenuList,menuItems:data.sysMap,userName:data.userName,userId:data.userId,roleName:data.roleName});

    request({
        url: pro.serveraddr+'/login/employeeLogin',
        method: "POST",
        qs: qs
    }, function(error, response, body){
        if (!error && response.statusCode == 200) {
            var data=JSON.parse(body);
            if(data && data.success){
                res.render('main/mainFrame', {title: '菜单', items: items});
                //res.render('main/index',{serverurl:pro.serveraddr,menus:data.parentMenuList,menuItems:data.sysMap,userName:data.userName,userId:data.userId,roleName:data.roleName});
            }else {
                console.log("error= " + data.msg );
                res.render('login');
            }
        }else{
            res.render('login');
        }
    });

});
router.get('/', function (req, res, next) {
    //ajax请求返回user对象
    // var user=null;
    // auth.dologin(req, res, next,user);
    var sid=auth.getSessionID(req, res, next);
    var qs=req.query;
    qs.sid=sid;
    //res.redirect('/index');
    // {serverurl:pro.serveraddr,menus:data.parentMenuList,menuItems:data.sysMap,userName:data.userName,userId:data.userId,roleName:data.roleName});
    res.render('main/mainFrame',{parentMenuList:false,sysMap:false,roleName:"",roleId:""});
    // request({
    //     url: pro.serveraddr+'/login/employeeLogin',
    //     method: "POST",
    //     qs: qs
    // }, function(error, response, body){
    //     if (!error && response.statusCode == 200) {
    //         var data=JSON.parse(body);
    //         if(data && data.success){
    //             console.log("登录信息>>"+body);
    //             res.render('main/mainFrame');
    //             //res.render('main/index',{serverurl:pro.serveraddr,menus:data.parentMenuList,menuItems:data.sysMap,userName:data.userName,userId:data.userId,roleName:data.roleName});
    //         }else {
    //             resultJSON.isSuccess = false;
    //             resultJSON.message = data.msg;
    //             console.log("error= " + data.msg);
    //             // res.redirect('/');
    //             res.json(resultJSON)
    //         }
    //     }else{
    //         res.redirect('/');
    //     }
    // });

});



module.exports = router;
